clear all 

set scheme s1mono

use "uncippo_final.dta", clear
keep if approved==1 | first_report_proposed_only==1

keep if umbrella1!="Support" & umbrella1!="NA" & umbrella1!="Trans. Auth"
tab umbrella1

*Match Sample: Africa from 1991 to 2016
kountry iso3n, from(iso3n) geo(un)
tab GEO
keep if GEO=="Africa"
keep if year>=1991 & year<=2016

*Code Democracy-relevant posts
*Dummy
gen dem_dummy=0
replace dem_dummy=1  if umbrella1=="Electoral Affairs" | umbrella1=="Political Affairs"
label var dem_dummy "Any Democracy Post"

*Size
tab umbrella1 
gen dem_posts_total=total if umbrella1=="Electoral Affairs" | umbrella1=="Political Affairs"
gen dem_intposts_total=international if umbrella1=="Electoral Affairs" | umbrella1=="Political Affairs"
gen dem_natposts_total=national if umbrella1=="Electoral Affairs" | umbrella1=="Political Affairs"
gen dem_posts_p=d2+p5p4+p3p2+p1 if umbrella1=="Electoral Affairs" | umbrella1=="Political Affairs"
gen dem_posts_nonp=total-(dem_posts_p) if umbrella1=="Electoral Affairs" | umbrella1=="Political Affairs"

label var dem_posts_total "Total Democracy Posts"
label var dem_intposts_total "International Democracy Posts"
label var dem_natposts_total "National Democracy Posts"
label var dem_posts_p "P-level Democracy Posts"
label var dem_posts_nonp "Non-P Democracy Posts"

*Pick one mission when multiple to the same country
drop if mission=="UNISFA" // pick UNMISS
drop if mission=="UNAMID" // pick UNMIS

foreach var of varlist dem_dummy-dem_posts_nonp{
	replace `var'=0 if `var'==.
}

collapse (max) dem_dummy (sum) dem_posts_nonp dem_posts_p dem_posts_total dem_intposts_total dem_natposts_total usgasg-national total international (first) country iso3n , by(cown year)
gen nondem_posts_total=total-dem_posts_total if dem_posts_total!=.
replace nondem_posts_total=total if nondem_posts_total==. & (dem_posts_total!=. | dem_posts_total==0)
label var nondem_posts_total "Total Non-Democracy Posts"

gen gwnoloc=cown
*Add 2 years to year to match years correctly
replace year=year+2
xtset gwnoloc year
save "BDSreplication/uncippo_democracy.dta", replace


use "BDSreplication/data.dta", clear
do "BDSreplication/prepare_bds.do"

merge 1:1 gwnoloc year using "BDSreplication/uncippo_democracy.dta"
tab country if _m==1
foreach var of varlist dem_dummy-internationalstaff{
	replace `var'=0 if `var'==. & _m==1 & iactual_civilian_total!=.
}

*start from 1993 as in BDS
foreach var of varlist dem_dummy-internationalstaff{
	replace `var'=. if year<=1992 | year>2017
}
 
drop _m

xtset gw year
la var iactual_civilian_total_K "BDS \# of civilian personnel (1K units)"

*Transform uncippo to compare
gen total_K=total/1000
gen international_K=international/1000
gen national_K=nationalst/1000

gen nondemposts_K=nondem_posts_total/1000
gen demposts_K=dem_posts_total/1000
gen demint_K=dem_intposts_total/1000
gen demnat_K=dem_natposts_total/1000
gen demp_K=dem_posts_p/1000
gen demnotp_K=dem_posts_nonp/1000
gen nondemnonp_K=total_K-(demp_K + demnotp_K)

foreach var of varlist total_K-nondemnonp_K{
	replace `var'=0 if `var'==.
}

la var total_K "UNCIPPO \# of civilian personnel (1K units)"
corr total_K iactual_civilian_total_K // 0.76 correlation

br country year total_K iactual_civilian_total_K
			
xtset gw year
label var total_K "UNCIPPO Total"
label var iactual_civilian_total_K "BDS' Total"

*Replication with BDS's code
*globals
gl ictrls_3l_varying iwdi_pop_3l iwdi_oda_3l iwdi_gdppc_3l iunhcr_ref_idp_3l iwdi_literacy_3l iwdi_fuel_3l
xtset gwnoloc year

*FIGURE 9
eststo clear
eststo: xtreg v2x_polyarchy  l2.total_K $ictrls_3l_varying, fe
eststo: xtreg v2x_polyarchy  l2.international_K l2.national_K $ictrls_3l_varying, fe
coefplot est1, keep(*total_K ) xline(0) legend(off) xscale(range(-0.1 0.15)) ylabel(1 "UNCIPPO Tot. Civilian Posts")
coefplot est2, keep(*international* *national*) xline(0) legend(off) xscale(range(-0.0001 .)) ylabel(1 "UNCIPPO Int. Civilian Posts" 2 "UNCIPPO Nat. Civilian Posts")

*FIGURE 10
eststo clear
eststo: xtreg v2x_polyarchy  l2.dem_dummy $ictrls_3l_varying, fe
eststo: xtreg v2x_polyarchy  l2.demposts_K l2.nondemposts_K $ictrls_3l_varying, fe
eststo: xtreg v2x_polyarchy  l2.demint_K l2.demnat_K $ictrls_3l_varying, fe
coefplot est1 est2,  keep(*dem_dummy *demposts_K *nondemposts_K) xline(0) legend(off) ylabel(1 "UNCIPPO Dem Unit" 2 "UNCIPPO Dem Posts" 3 "UNCIPPO Non-Dem Posts")
coefplot est3, keep(*demint_K *demnat_K) xline(0) legend(off) ylabel(1 "UNCIPPO Dem Int." 2 "UNCIPPO Dem Nat.")


*FIGURE 11
eststo clear
eststo: xtreg v2x_polyarchy  l2.demp_K l2.demnotp_K $ictrls_3l_varying, fe
coefplot est1, keep( *demp_K *demnotp_K) xline(0) xline(0) legend(off) ylabel(1  "UNCIPPO Dem P+" 2 "UNCIPPO Dem non-P+")
